/*
 * @Copyright    : Blueray Med .ltd
 * @file name    : Filename
 * @Date         : 2022-10-19 16:03:50
 * @LastEditor   : Liam
 * @Description  : 按键按下后的处理函数
 */
#include "btn_handler.h"
#include "myQueue.h"

/**
 * @Function: page_to_page_handler_function
 * @Description: 处理页面跳转的函数，含有准备与待机切换的函数。
 * @Author: Liam
 * @Input: pagenum_now_val传入现在的页面数字；pagenum_btn_val传入需要跳转的页面数字。
 * @Return: null
 * @Others: note
 * @result: Success
 */
void page_to_page_handler_function(u16 pagenum_now_val, u16 pagenum_btn_val)
{
    switch (pagenum_now_val)
    {
    case PIC_NUM_KPA_MAIN:
        if (pagenum_btn_val == PIC_NUM_KPA_READY)
        {
            pic_set(PIC_NUM_KPA_READY);
            enter_ready();
        }
        else if (pagenum_btn_val == PIC_NUM_HG_MAIN) /* 切换单位 */
        {
            switching_units(PIC_NUM_HG_MAIN);
            pic_set(PIC_NUM_HG_MAIN);
            send_units_handler(PIC_NUM_HG_MAIN);
        }
        else if (pagenum_btn_val == PIC_NUM_CALIBRATE)
        {
            pic_set(PIC_NUM_PASSWORD0);
            enter_set();
        }
        break;
    case PIC_NUM_KPA_READY:
        if (pagenum_btn_val == PIC_NUM_KPA_MAIN)
        {
            pic_set(PIC_NUM_KPA_MAIN);
            exit_ready();
        }
        else if (pagenum_btn_val == PIC_NUM_HG_READY) /* 切换单位 */
        {
            switching_units(PIC_NUM_HG_READY);
            pic_set(PIC_NUM_HG_READY);
            send_units_handler(PIC_NUM_HG_READY);
        }
        break;
    case PIC_NUM_HG_MAIN:
        if (pagenum_btn_val == PIC_NUM_KPA_MAIN) /* 切换单位 */
        {
            switching_units(PIC_NUM_KPA_MAIN);
            pic_set(PIC_NUM_KPA_MAIN);
            send_units_handler(PIC_NUM_KPA_MAIN);
        }
        else if (pagenum_btn_val == PIC_NUM_HG_READY)
        {
            pic_set(PIC_NUM_HG_READY);
            enter_ready();
        }
        break;
    case PIC_NUM_HG_READY:
        if (pagenum_btn_val == PIC_NUM_HG_MAIN)
        {
            pic_set(PIC_NUM_HG_MAIN);
            exit_ready();
        }
        else if (pagenum_btn_val == PIC_NUM_KPA_READY) /* 切换单位 */
        {
            switching_units(PIC_NUM_KPA_READY);
            pic_set(PIC_NUM_KPA_READY);
            send_units_handler(PIC_NUM_KPA_READY);
        }
        break;
    case PIC_NUM_CALIBRATE:
        if (pagenum_btn_val == PIC_NUM_KPA_MAIN)
        {
            pic_set(PIC_NUM_KPA_MAIN);
            exit_set();
        }
        else if (pagenum_btn_val == PIC_NUM_KPA_READY)
        {
            pic_set(PIC_NUM_KPA_READY);
        }
        break;
    case PIC_NUM_PASSWORD0:
        if (pagenum_btn_val == PIC_NUM_KPA_MAIN)
        {
            pic_set(PIC_NUM_KPA_MAIN);
        }

    default:
        break;
    }
}

/**
 * @Function: enter_set
 * @Description: 进入标定界面
 * @Author: Liam
 * @Input:
 * @Return: null
 * @Others: note
 * @result: Success
 */
void enter_set(void)
{
    my_queue_enqueue(PROTOCOL_READ, SET_PAGE_DATA, 40, SET_KP, 3, WARNING_VALUE, 2, PTR_NUM_3); /* 读取参数 */
}

/**
 * @Function: exit_set
 * @Description: 退出标定界面
 * @Author: Liam
 * @Input:
 * @Return: null
 * @Others: note
 * @result: Success
 */
void exit_set(void)
{
    write_VP_check(SET_PAGE_PRESSURE_SWITCH, 0);
    write_VP_check(SET_PAGE_SPEED_SWITCH, 0);
    nor_flash_rw_cmd(NOR_FLASH_W, 0x00000002, BC_MODE_ADDR, 0x0008);                                    /* 保存型号 */
    my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_PRESSURE_SWITCH, 1, SET_PAGE_SPEED_SWITCH, 1, PTR_NUM_2); /* 发送参数 */
}

/**
 * @Function: enter_ready
 * @Description: 进入准备状态处理函数
 * @Author: Liam
 * @Input:
 * @Return: null
 * @Others: note
 * @result: Success
 */
void enter_ready(void)
{
    write_VP_check(READY_BTN_ADDR, OPEN);
    my_queue_enqueue(PROTOCOL_WRITE, READY_BTN_ADDR, 1, PTR_NUM_1); /* 发送参数 */
}

/**
 * @Function: exit_ready
 * @Description: 退出准备状态
 * @Author: Liam
 * @Input: null
 * @Return: null
 * @Others: note
 * @result: Success
 */
void exit_ready()
{
    write_VP_check(READY_BTN_ADDR, CLOSE);
    my_queue_enqueue(PROTOCOL_WRITE, READY_BTN_ADDR, 1, PTR_NUM_1); /* 发送参数 */
}
/**
 * @Function: set_max_flow
 * @Description: 设置最大流量
 * @Author: Liam
 * @Input: null
 * @Return: null
 * @Others: note
 * @result: Success
 */
void set_max_flow()
{
    u16 max_flow;
    u16 flow;
    sys_read_vp(BC_MODE_ADDR, (u8 *)&max_flow, 1); /* 读取模式 */
    sys_read_vp(FLOW_SET_BTN, (u8 *)&flow, 1);

    if (max_flow == 1) /* 低流量 */
    {
        if (flow <= 800)/* 流量低于800正常 */
        {
            write_VP_check(SET_FLOW, flow);
        }
        else /* 流量高于800 */
        {
            write_VP_check(FLOW_SET_BTN, 800);
        }
    }
    else /* 高流量 */
    {
        write_VP_check(SET_FLOW, flow);
    }
}

/**
 * @Function: send_pressure_flow_handler
 * @Description: 发送改变的值
 * @Author: Liam
 * @Input: null
 * @Return: null
 * @Others: note
 * @result: Success
 */
void send_pressure_flow_handler(void)
{
    u16 pressure;
    u16 flow;
    u16 late_pressure;
    u16 late_flow;

    u16 pressure_switch_val;
    u16 late_pressure_switch_val;
    u16 set_pressure;
    u16 late_set_pressure;
    u16 set_flow;
    u16 late_set_flow;

    u16 set_speed;
    u16 late_set_speed;
    u16 speed_switch_val;
    u16 late_speed_switch_val;
    /* 标定页面压力流量 */
    sys_read_vp(SET_PAGE_PRESSURE, (u8 *)&set_pressure, 1);
    sys_read_vp(SET_PAGE_FLOW, (u8 *)&set_flow, 1);
    sys_read_vp(SET_PAGE_PRESSURE_SWITCH, (u8 *)&pressure_switch_val, 1);
    /* 标定界面转速 */
    sys_read_vp(SET_PAGE_SPEED, (u8 *)&set_speed, 1);
    sys_read_vp(SET_PAGE_SPEED_SWITCH, (u8 *)&speed_switch_val, 1);
    /* 主页面 */
    sys_read_vp(SET_MAIN_PRESSURE, (u8 *)&pressure, 1);
    sys_read_vp(SET_FLOW, (u8 *)&flow, 1);
    if (sysSt.pagenum_now_val >= PIC_NUM_KPA_MAIN && sysSt.pagenum_now_val <= PIC_NUM_HG_READY) /* 在使用界面发送流量与压力 */
    {
        if (late_flow != flow)
        {
            late_flow = flow;
            my_queue_enqueue(PROTOCOL_WRITE, SET_FLOW, 1, PTR_NUM_1); /* 发送参数 */
        }
        if (late_pressure != pressure)
        {
            late_pressure = pressure;
            my_queue_enqueue(PROTOCOL_WRITE, SET_MAIN_PRESSURE, 1, PTR_NUM_1); /* 发送参数 */
        }
    }
    else if (sysSt.pagenum_now_val == PIC_NUM_CALIBRATE)/* 在标定页面 */
    {
        if (late_pressure_switch_val != pressure_switch_val) /* 压力流量开关 */
        {
            late_pressure_switch_val = pressure_switch_val;
            if (pressure_switch_val == 1) /* 开关为开 */
            {
                my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_PRESSURE_SWITCH, 1, SET_PAGE_PRESSURE, 2, PTR_NUM_2); /* 发送参数 */
                if (late_set_pressure != set_pressure)
                {
                    late_set_pressure = set_pressure;
                    my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_PRESSURE, 1, PTR_NUM_1); /* 发送参数 */
                }
                if (late_set_flow != set_flow)
                {
                    late_set_flow = set_flow;
                    my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_FLOW, 1, PTR_NUM_1); /* 发送参数 */
                }
            }
            else if (pressure_switch_val == 0)
            {
                my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_PRESSURE_SWITCH, 1, PTR_NUM_1); /* 发送参数 */
            }
        }
        if (late_speed_switch_val != speed_switch_val) /* 转速开关 */
        {
            late_speed_switch_val = speed_switch_val;
            if (speed_switch_val == 1) /* 开关为开 */
            {
                my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_SPEED, 2, PTR_NUM_1); /* 发送参数 */
                if (late_set_speed != set_speed)
                {
                    late_set_speed = set_speed;
                    my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_SPEED, 1, PTR_NUM_1); /* 发送参数 */
                }
            }
            else if (speed_switch_val == 0)
            {
                my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_SPEED_SWITCH, 1, PTR_NUM_1); /* 发送参数 */
            }
        }
    }
}

/**
 * @Function: warning_handler
 * @Description: 报警处理
 * @Author: Liam
 * @Input: 
 * @Return: null
 * @Others: note
 * @result: Success
 */
void warning_handler(void)
{
    u16 warning_flag;
    sys_read_vp(WARNING_FLAG, (u8 *)&warning_flag, 1);
    /* 屏幕蜂鸣器报警 */
    // if (warning_flag == 1)
    // {
    //     write_VP_check(BEEP_ADDR, BEEP_TIME_400);
    // }
}

/**
 * @Function: switching_units
 * @Description: 转换单位
 * @Author: Liam
 * @Input: 
 * @Return: null
 * @Others: 转换单位两种模式，一种是KPA，一种是mmHg
 * @result: Success
 */
void switching_units(u16 page_num)
{
    u16 kpa;
    u16 mmhg;
    u16 pressure;
    sys_read_vp(SET_MAIN_PRESSURE, (u8 *)&pressure, 1);
    if (page_num == PIC_NUM_KPA_READY || page_num == PIC_NUM_KPA_MAIN)
    {
        kpa = pressure * 1.333;
        write_VP_check(SET_MAIN_PRESSURE, kpa);
    }
    else if (page_num == PIC_NUM_HG_READY || page_num == PIC_NUM_HG_MAIN)
    {
        mmhg = pressure *0.752;
        write_VP_check(SET_MAIN_PRESSURE, mmhg);
    }
}


/**
 * @Function: send_units_handler
 * @Description: 发送单位选择
 * @Author: Liam
 * @Input: page_num 根据页面编号发送响应的单位
 * @Return: null
 * @Others: note
 * @result: Success
 */
void send_units_handler(u16 page_num)
{
    if (page_num == PIC_NUM_KPA_READY || page_num == PIC_NUM_KPA_MAIN)
    {
        write_VP_check(SET_UNITS, UNITS_KPA);
        my_queue_enqueue(PROTOCOL_WRITE, SET_UNITS, 1, PTR_NUM_1); /* 发送参数 */
    }
    else if (page_num == PIC_NUM_HG_READY || page_num == PIC_NUM_HG_MAIN)
    {
        write_VP_check(SET_UNITS, UNITS_MMHG);
        my_queue_enqueue(PROTOCOL_WRITE, SET_UNITS, 1, PTR_NUM_1); /* 发送参数 */
    }
}

/**
 * @Function: save_btn_handler
 * @Description: 保存标定数据
 * @Author: Liam
 * @Input:
 * @Return: null
 * @Others: note
 * @result: Success
 */
void save_btn_handler(void)
{
    u16 save_btn_val;
    sys_read_vp(SAVE_BTN_ADDR, (u8 *)&save_btn_val, 1);
    if (save_btn_val == 1)
    {
        write_VP_check(SAVE_SEND_ADDR, 1);
        my_queue_enqueue(PROTOCOL_WRITE, SET_PAGE_DATA, 40, SET_KP, 3, SAVE_SEND_ADDR, 1, WARNING_VALUE, 2, PTR_NUM_4); /* 发送参数 */
        write_VP_check(SAVE_BTN_ADDR, 0);
    }
}

/**
 * @Function: long_press_init
 * @Description: 长按按键初始化
 * @Author: Liam
 * @Input:
 * @Return: null
 * @Others: note
 * @result: Success
 */
void long_press_init(void)
{
    write_VP_check(TP_ON, 1);
    write_VP_check(TP_Continue, 2);
    write_VP_check(TP_OFF, 0);
}

/**
 * @Function: long_press_function
 * @Description: 按键长按处理
 * @Author: Liam
 * @Input:
 * @Return: null
 * @Others: note
 * @result: Success
 */
void long_press_function(page_num)
{
#define KPA_MAX_VAL 533
#define HG_MAX_VAL 400
    u16 add_btn_state;
    u16 subtract_btn_state;
    u16 pressure_val;
    sys_read_vp(TP_ADD_RETURN, (u8 *)&add_btn_state, 1);
    sys_read_vp(TP_SUBTRACT_RETURN, (u8 *)&subtract_btn_state, 1);
    sys_read_vp(SET_MAIN_PRESSURE, (u8 *)&pressure_val, 1);
    if (add_btn_state == 1) /* 单击+ */
    {
        if (page_num == PIC_NUM_KPA_READY || page_num == PIC_NUM_KPA_MAIN)
        {
            if (pressure_val < KPA_MAX_VAL)
            {
                pressure_val++;
            }
            else if (pressure_val >= KPA_MAX_VAL)
            {
                pressure_val = KPA_MAX_VAL;
            }
        }
        else if (page_num == PIC_NUM_HG_READY || page_num == PIC_NUM_HG_MAIN)
        {
            if (pressure_val < HG_MAX_VAL)
            {
                pressure_val++;
            }
            else if (pressure_val >= HG_MAX_VAL)
            {
                pressure_val = HG_MAX_VAL;
            }
        }
        write_VP_check(SET_MAIN_PRESSURE, pressure_val);
    }
    else if (add_btn_state == 2) /* 长按+ */
    {
        if (page_num == PIC_NUM_KPA_READY || page_num == PIC_NUM_KPA_MAIN)
        {
            if (pressure_val < KPA_MAX_VAL)
            {
                if (pressure_val % 10 == 0) /* 整数 */
                {
                    pressure_val += 10;
                }
                else /* 小数 */
                {
                    pressure_val++;
                }
            }
            else if (pressure_val >= KPA_MAX_VAL)
            {
                pressure_val = KPA_MAX_VAL;
            }
        }
        else if (page_num == PIC_NUM_HG_READY || page_num == PIC_NUM_HG_MAIN)
        {
            if (pressure_val < HG_MAX_VAL)
            {
                if (pressure_val % 10 == 0) /* 整数 */
                {
                    pressure_val += 10;
                }
                else /* 小数 */
                {
                    pressure_val++;
                }
            }
            else if (pressure_val >= HG_MAX_VAL)
            {
                pressure_val = HG_MAX_VAL;
            }
        }
        write_VP_check(SET_MAIN_PRESSURE, pressure_val);
    }
    else if (subtract_btn_state == 1) /* 单击- */
    {
        if (pressure_val > 0 && pressure_val <= KPA_MAX_VAL)
        {
            pressure_val--;
        }
        else if (pressure_val == 0 || pressure_val> KPA_MAX_VAL)
        {
            pressure_val = 0;
        }
        write_VP_check(SET_MAIN_PRESSURE, pressure_val);
    }
    else if (subtract_btn_state == 2) /* 长按- */
    {
        if (pressure_val > 0 && pressure_val <= KPA_MAX_VAL)
        {
            if (pressure_val % 10 == 0) /* 整数 */
            {
                pressure_val -= 10;
            }
            else /* 小数 */
            {
                pressure_val--;
            }
        }
        else if (pressure_val == 0 || pressure_val> KPA_MAX_VAL)
        {
            pressure_val = 0;
        }
        write_VP_check(SET_MAIN_PRESSURE, pressure_val);
    }
}
/**
 * @Function: the_gui_system_init
 * @Description: 系统开机初始化的默认参数写入
 * @Author: Liam
 * @Input: null
 * @Return: null
 * @Others: note
 * @result: Success
 */
void the_gui_system_init(void)
{
    /* 开机打开按键声音调整屏幕的显示角度 */
    sys_the_sound(OPEN);
    sys_delay_about_ms(10);
    /* 语言型号读取 */
    the_language_enable();
    /* 开机动画 */
    pic_set(PIC_NUM_OPEN_ACTION);
    sysSt.open_sec = 0;
    write_VP_check(OPEN_ACTION, OPEN);
    /* 型号区分初始化 */
    write_VP_check(FLOW_SET_BTN, 50);
    /* 长按按键初始化 */
    long_press_init();
}
